from django.db import models
from django.db.models import permalink
from django.contrib.auth.models import User
from places.models import City

import datetime
from dateutil import relativedelta
import re


class Profile(models.Model):
  """ Profile model """
  GENDER_CHOICES = (
    (1, 'Male'),
    (2, 'Female'),
  )
  user              = models.ForeignKey(User)
  gender            = models.PositiveSmallIntegerField(choices=GENDER_CHOICES, blank=True, null=True)
  mugshot           = models.FileField(upload_to='mugshots', blank=True)
  birth_date        = models.DateField(blank=True, null=True)
  address1          = models.CharField(blank=True, max_length=100)
  address2          = models.CharField(blank=True, max_length=100)
  city              = models.ForeignKey(City, blank=True, null=True)
  zip               = models.CharField(blank=True, max_length=10)
  country           = models.CharField(blank=True, max_length=100)
  mobile            = models.PhoneNumberField(blank=True, null=True)
  mobile_provider   = models.ForeignKey('MobileProvider', blank=True, null=True)
  
  class Meta:
    db_table = 'user_profiles'

  class Admin:
    pass

  def __str__(self):
    return '%s' % self.user.get_full_name()
  
  @property
  def age(self):
    TODAY = datetime.date.today()
    return '%s' % relativedelta.relativedelta(TODAY, self.birth_date).years
  
  @property
  def full_name(self):
    return '%s' % self.user.get_full_name()
  
  @permalink
  def get_absolute_url(self):
    return ('profile_detail', None, { 'object_id': self.id })
  
  @property
  def sms_address(self):
    if (self.mobile and self.mobile_provider):
      return '%s@%s' % (re.sub('-', '', self.mobile), self.mobile_provider.domain)


class MobileProvider(models.Model):
  """ MobileProvider model """
  title             = models.CharField(max_length=25)
  domain            = models.CharField(max_length=50)

  class Meta:
    db_table = 'user_mobile_providers'

  class Admin:
    pass

  def __str__(self):
    return '%s' % self.title


class ServiceType(models.Model):
  """ Service type model """
  title           = models.CharField(blank=True, max_length=100)
  url             = models.URLField(help_text='Be sure to use ending slash. (e.g. http://www.flickr.com/)')

  class Meta:
    db_table = 'user_service_types'

  class Admin:
    pass

  def __str__(self):
    return '%s' % self.title


class Service(models.Model):
  """ Service model """
  service         = models.ForeignKey(ServiceType)
  user            = models.ForeignKey(User)
  username        = models.CharField(max_length=100)
  created         = models.DateTimeField(auto_now_add=True)
  modified        = models.DateTimeField(auto_now=True)

  class Meta:
    db_table = 'user_services'

  class Admin:
    pass

  def __str__(self):
    return '%s' % self.username
  
  @property
  def service_url(self):
    return '%s%s' % (self.service.url, self.username)
  
  @property
  def title(self):
    return '%s' % self.service.title

class Link(models.Model):
  """ Service type model """
  user            = models.ForeignKey(User)
  title           = models.CharField(max_length=100)
  url             = models.URLField(verify_exists=True)

  class Meta:
    db_table = 'user_links'

  class Admin:
    pass

  def __str__(self):
    return '%s' % self.title